package com.share.sharead.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.share.sharead.R;

import butterknife.ButterKnife;

/**
 * Created : Yx
 * Time : 2019/6/18 17:38
 * Describe :
 */

public class DialogOpenAnim extends Dialog implements View.OnClickListener {
    public interface OnDialogItemClick {
        void onItem1Clicks();
    }

    private Context mContext;
    private static DialogOpenAnim dialogOpenAnim;
    private OnDialogItemClick mClicks;

    private ImageView anim0, anim1, anim2, anim3, anim4, boss;
    private RelativeLayout clickOut;
    private boolean isOpen = false;


    private DialogOpenAnim(Context context, int themeResId, OnDialogItemClick itemClick) {
        super(context, themeResId);
        this.mContext = context;
        this.mClicks = itemClick;
    }

    public static void showDialog(Context context, OnDialogItemClick itemClick) {
        dialogOpenAnim = new DialogOpenAnim(context, R.style.dialog_openanim_style, itemClick);
        dialogOpenAnim.show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigation_open_anim);
        ButterKnife.bind(this);
        Window dialogWindow = getWindow();
        assert dialogWindow != null;
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = AbsListView.LayoutParams.MATCH_PARENT;
        lp.y = 0;//设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp);
        anim0 = findViewById(R.id.anim0);
        anim1 = findViewById(R.id.anim1);
        anim2 = findViewById(R.id.anim2);
        anim3 = findViewById(R.id.anim3);
        anim4 = findViewById(R.id.anim4);
        clickOut = findViewById(R.id.clickOut);
        boss = findViewById(R.id.boss);
        anim0.setOnClickListener(this);
        anim1.setOnClickListener(this);
        anim2.setOnClickListener(this);
        anim3.setOnClickListener(this);
        anim4.setOnClickListener(this);
        clickOut.setOnClickListener(this);
        boss.setOnClickListener(this);
        //进入页面等300ms之后再进行动画

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                boss.setClickable(false);//禁用点击，防止快速点击的时候动画未完成就再次开始
                if (!isOpen) {
                    isOpen = !isOpen;//禁止快速点击
                    animRun(anim0, anim1, anim2, anim3, anim4);
                }
            }
        }, 500);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clickOut://点击任意空白处均可关闭此页面
            case R.id.boss:
                if (isOpen) {
                    isOpen = !isOpen;
                    animRunBack(anim0, anim1, anim2, anim3, anim4);//关闭时开启收回动画
                }
                break;
            case R.id.anim0:
                if (isOpen) {
                    isOpen = !isOpen;
                    animRunBack(anim0, anim1, anim2, anim3, anim4);//关闭时开启收回动画
                }
                mClicks.onItem1Clicks();
                break;
            case R.id.anim1:
                if (isOpen) {
                    isOpen = !isOpen;
                    animRunBack(anim0, anim1, anim2, anim3, anim4);//关闭时开启收回动画
                }
                mClicks.onItem1Clicks();
                break;
            case R.id.anim2:
                if (isOpen) {
                    isOpen = !isOpen;
                    animRunBack(anim0, anim1, anim2, anim3, anim4);//关闭时开启收回动画
                }
                mClicks.onItem1Clicks();
                break;
            case R.id.anim3:
                if (isOpen) {
                    isOpen = !isOpen;
                    animRunBack(anim0, anim1, anim2, anim3, anim4);//关闭时开启收回动画
                }
                mClicks.onItem1Clicks();
                break;
            case R.id.anim4:
                if (isOpen) {
                    isOpen = !isOpen;
                    animRunBack(anim0, anim1, anim2, anim3, anim4);//关闭时开启收回动画
                }
                mClicks.onItem1Clicks();
                break;
        }
    }


    //菜单展开的动画
    private void animRun(final View view0, final View view1, final View view2, final View view3, final View view4) {
        //获取屏幕宽度
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        final int screenWidth = dm.widthPixels;

        //左下控件的动画
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(view0, "translationY",
                0.0F, -screenWidth / 7f).setDuration(300);//Y方向移动距离
        ObjectAnimator animator01 = ObjectAnimator.ofFloat(view0, "translationX",
                0.0F, -screenWidth / 2.5f).setDuration(300);//X方向移动距离
        ObjectAnimator animator02 = ObjectAnimator.ofFloat(view0, "scaleX", 1.0f, 1.5f).setDuration(300);//X方向放大
        ObjectAnimator animator03 = ObjectAnimator.ofFloat(view0, "scaleY", 1.0f, 1.5f).setDuration(300);//Y方向放大
        AnimatorSet animSet0 = new AnimatorSet();
        animSet0.setInterpolator(new OvershootInterpolator());//到达指定位置后继续向前移动一定的距离然后弹回指定位置,达到颤动的特效
        animSet0.playTogether(animator0, animator01, animator02, animator03);//四个动画同时执行

        //左边控件的动画
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view1, "translationY",
                0.0F, -screenWidth / 3f).setDuration(300);//Y方向移动距离
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view1, "translationX",
                0.0F, -screenWidth / 4f).setDuration(300);//X方向移动距离
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view1, "scaleX", 1.0f, 1.5f).setDuration(300);//X方向放大
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(view1, "scaleY", 1.0f, 1.5f).setDuration(300);//Y方向放大
        final AnimatorSet animSet1 = new AnimatorSet();
        animSet1.setInterpolator(new OvershootInterpolator());//到达指定位置后继续向前移动一定的距离然后弹回指定位置,达到颤动的特效
        animSet1.playTogether(animator1, animator2, animator3, animator4);//四个动画同时执行

        //中间控件的动画,因需要设置监听所以与
        final ObjectAnimator animator5 = ObjectAnimator.ofFloat(view2, "translationY",
                0.0F, -screenWidth / 2f).setDuration(300);
        ObjectAnimator animator6 = ObjectAnimator.ofFloat(view2, "scaleX", 1.0f, 1.5f).setDuration(300);
        ObjectAnimator animator7 = ObjectAnimator.ofFloat(view2, "scaleY", 1.0f, 1.5f).setDuration(300);
        final AnimatorSet animatorSet2 = new AnimatorSet();
        animatorSet2.setInterpolator(new OvershootInterpolator());
        animatorSet2.playTogether(animator5, animator6, animator7);
        animatorSet2.setStartDelay(50);//监听第一个动画开始之后50ms开启第二个动画,达到相继弹出的效果

        //右侧控件的动画
        ObjectAnimator animator8 = ObjectAnimator.ofFloat(view3, "translationY",
                0.0F, -screenWidth / 3).setDuration(300);
        ObjectAnimator animator9 = ObjectAnimator.ofFloat(view3, "translationX",
                0.0F, screenWidth / 4f).setDuration(300);
        ObjectAnimator animator10 = ObjectAnimator.ofFloat(view3, "scaleX", 1.0f, 1.5f).setDuration(300);
        ObjectAnimator animator11 = ObjectAnimator.ofFloat(view3, "scaleY", 1.0f, 1.5f).setDuration(300);
        final AnimatorSet animatorSet3 = new AnimatorSet();
        animatorSet3.setInterpolator(new OvershootInterpolator());
        animatorSet3.playTogether(animator8, animator9, animator10, animator11);
        animatorSet3.setStartDelay(50);

        //右下侧控件的动画
        ObjectAnimator animator12 = ObjectAnimator.ofFloat(view4, "translationY",
                0.0F, -screenWidth / 7f).setDuration(300);
        ObjectAnimator animator13 = ObjectAnimator.ofFloat(view4, "translationX",
                0.0F, screenWidth / 2.5f).setDuration(300);
        ObjectAnimator animator14 = ObjectAnimator.ofFloat(view4, "scaleX", 1.0f, 1.5f).setDuration(300);
        ObjectAnimator animator15 = ObjectAnimator.ofFloat(view4, "scaleY", 1.0f, 1.5f).setDuration(300);
        final AnimatorSet animatorSet4 = new AnimatorSet();
        animatorSet4.setInterpolator(new OvershootInterpolator());
        animatorSet4.playTogether(animator12, animator13, animator14, animator15);
        animatorSet4.setStartDelay(50);


        //第四个动画结束之后设置boss按键可点击,点击即收回动画
        animatorSet4.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                boss.setClickable(true);
            }
        });
        //第三个开始之后再开启第四个
        animatorSet3.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animatorSet4.start();
            }

        });
        //第二个开始之后再开启第三个
        animatorSet2.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animatorSet3.start();
            }

        });

        //第一个动画开始之后再开启第二个
        animSet1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animatorSet2.start();
            }
        });


        //第0个动画开始之后再开启第二个
        animSet0.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animSet1.start();//放在最后是为了初始化完毕所有的动画之后才触发第一个控件的动画
            }
        });

        animSet0.start();//放在最后 :  是为了初始化完毕所有的动画之后才触发第一个控件的动画

    }


    //收回动画,相当于反向执行展开动画,此处不做更详细的注释
    private void animRunBack(final View view0, final View view1, final View view2, final View view3, final View view4) {
        //获取屏幕宽度
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        final int screenWidth = dm.widthPixels;

        //第0个收回动画
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(view0, "translationY",
                -screenWidth / 7f, 0.0F).setDuration(300);//Y方向移动距离
        ObjectAnimator animator01 = ObjectAnimator.ofFloat(view0, "translationX",
                -screenWidth / 2.5f, 0.0F).setDuration(300);//X方向移动距离
        ObjectAnimator animator02 = ObjectAnimator.ofFloat(view0, "scaleX", 1.5f, 1.0f).setDuration(300);//X方向放大
        ObjectAnimator animator03 = ObjectAnimator.ofFloat(view0, "scaleY", 1.5f, 1.0f).setDuration(300);//Y方向放大
        AnimatorSet animSet0 = new AnimatorSet();
        animSet0.setInterpolator(new DecelerateInterpolator());
        animSet0.playTogether(animator0, animator01, animator02, animator03);

        //第一个收回动画
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view1, "translationY",
                -screenWidth / 3f, 0.0F).setDuration(300);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view1, "translationX",
                -screenWidth / 4f, 0.0F).setDuration(300);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view1, "scaleX", 1.5f, 1.0f).setDuration(300);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(view1, "scaleY", 1.5f, 1.0f).setDuration(300);
        final AnimatorSet animSet = new AnimatorSet();
        animSet.setInterpolator(new DecelerateInterpolator());
        animSet.playTogether(animator1, animator2, animator3, animator4);

        //第二个收回动画
        final ObjectAnimator animator5 = ObjectAnimator.ofFloat(view2, "translationY",
                -screenWidth / 2f, 0.0F).setDuration(300);
        ObjectAnimator animator6 = ObjectAnimator.ofFloat(view2, "scaleX", 1.5f, 1.0f).setDuration(300);
        ObjectAnimator animator7 = ObjectAnimator.ofFloat(view2, "scaleY", 1.5f, 1.0f).setDuration(300);
        //                animator3.setInterpolator(new OvershootInterpolator());
        final AnimatorSet animatorSet2 = new AnimatorSet();
        animatorSet2.setInterpolator(new DecelerateInterpolator());
        animatorSet2.playTogether(animator5, animator6, animator7);
        animatorSet2.setStartDelay(50);

        //第三个收回动画
        ObjectAnimator animator8 = ObjectAnimator.ofFloat(view3, "translationY",
                -screenWidth / 3, 0.0F).setDuration(300);
        ObjectAnimator animator9 = ObjectAnimator.ofFloat(view3, "translationX",
                screenWidth / 4f, 0.0F).setDuration(300);
        ObjectAnimator animator10 = ObjectAnimator.ofFloat(view3, "scaleX", 1.5f, 1.0f).setDuration(300);
        ObjectAnimator animator11 = ObjectAnimator.ofFloat(view3, "scaleY", 1.5f, 1.0f).setDuration(300);
        final AnimatorSet animatorSet3 = new AnimatorSet();
        animatorSet3.setInterpolator(new DecelerateInterpolator());
        animatorSet3.playTogether(animator8, animator9, animator10, animator11); //四个动画同时执行
        animatorSet3.setStartDelay(50);
        //第四个收回动画
        ObjectAnimator animator12 = ObjectAnimator.ofFloat(view4, "translationY",
                -screenWidth / 7f, 0.0F).setDuration(300);
        ObjectAnimator animator13 = ObjectAnimator.ofFloat(view4, "translationX",
                screenWidth / 2.5f, 0.0F).setDuration(300);
        ObjectAnimator animator14 = ObjectAnimator.ofFloat(view4, "scaleX", 1.5f, 1.0f).setDuration(300);
        ObjectAnimator animator15 = ObjectAnimator.ofFloat(view4, "scaleY", 1.5f, 1.0f).setDuration(300);
        final AnimatorSet animatorSet4 = new AnimatorSet();
        animatorSet4.setInterpolator(new DecelerateInterpolator());
        animatorSet4.playTogether(animator12, animator13, animator14, animator15); //四个动画同时执行
        animatorSet4.setStartDelay(50);


        //0号动画的animator3开始执行时，执行4号动画
        animator3.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                //执行4号动画
                animatorSet4.start();
            }
        });

        //4号开始后，执行3号动画
        animatorSet4.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animatorSet3.start();
            }
        });
        //3号动画结束监听
        animatorSet3.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //finish();//收回动画结束后finish此页面
                dialogOpenAnim.dismiss();
            }
        });

        //1号执行完毕，开始2号
        animSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animatorSet2.start();
            }
        });
        //0号执行完毕，开始1号
        animSet0.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                animSet.start();
            }
        });
        //0号开始关闭
        animSet0.start();
    }

    @Override
    public void onBackPressed() {
        Log.i("aaaa", "  Dialog返回    ");
        if (isOpen) {
            isOpen = !isOpen;
            boss.setClickable(false);
            animRunBack(anim0, anim1, anim2, anim3, anim4);
        }
        //super.onBackPressed();
    }

}
